<?php
namespace app\common\logic;
use think\facade\Db;

class SmsApiLogic{
	static function getPhone($method, $info=[]){
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('function', $method)
					->find();
		if(!$apiInfo){
			return false;
		}
		$url = $apiInfo['url'];
		if($apiInfo['params']){
			//组合请求地址
			$url .= "?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{apiid}', $info['api_id'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
		}
		sys_log($url,'getPhone.log');
		$rs = curlGet($url);
		$data = json_decode($rs, true);
		sys_log($rs,'getPhone.log');
		return $data;
	}
	static function  getAllCounts($method='', $channel_id, $country){
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $channel_id)
					->where('function', $method)
					->find();
		if(!$apiInfo){
			return false;
		}
		$url = $apiInfo['url'];
		if($apiInfo['params']){
			//组合请求地址
			$url .= "?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{country}', $country['country_code'], $url);
		}
		
		$rs = curlGet($url);
		$data = json_decode($rs, true);

		$total_data = [];
		$total_name = "Total_".$country['country_code'];
		if($data){
			foreach($data as $k=>$v){
				if($v['Country']==$country['country_code'] && $v['Operator']==$total_name){
					$total_data = $v['Services'];
					break;
				}
			}
		}
		return $total_data;
	}
	//获取国家对应的服务列表的价格
	static function getPricesByCountry($method='', $channel_id, $country){
		//获取API信息
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $channel_id)
					->where('channel_type', $country['channel_type'])
					->where('function', $method)
					->find();

		//组合请求地址
		$url = $apiInfo['url'];
		if($apiInfo['params']){
			//组合请求地址
			$url .= "?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{country}', $country['country_code'], $url);
		}

		$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$channel_id)
				->orderRaw("rand()")
				->find();
		if($keys){
			$url = str_replace('{apiKey}', $keys['key'], $url);
		}

		$header = [];
		if($apiInfo['header']){
			$headerStr = $apiInfo['header'];
			$headerStr = str_replace('{apiKey}', $keys['key'], $headerStr);
			$header = explode('|', $headerStr);
			sys_log(json_encode($header),'header.log');
		}
		
		sys_log($url,'方法通道.log');
		if($channel_id==4){
			$r_url = "http://47.251.42.169/api.php?url=".urlencode($url);
			$rs = file_get_contents($r_url);
			$data = self::td4cl($rs);
			// sys_log($rs,'方法通道.log');
		}else{
			$rs = curlGet($url);
			$data = json_decode($rs, true);
		}
		return $data;
	}

	static function td4cl($rs){
		$data = explode('<br>', $rs);
		$lst = [];
		// echo '<pre>';
		foreach($data as $k=>$v){
			$tmp = explode('|', $v);
			if(!$tmp[0]){
				continue;
			}
			$tmp2['server_code'] = $tmp[0];
			$tmp2['server_name'] = $tmp[1];
			for($i=2;$i<count($tmp);$i++){
				$tmp3 = explode('-', $tmp[$i]);
				if(!isset($tmp3[1])){
					continue;
				}
				$tmp2['country_code'] = $tmp3[0];
				$tmp2['price'] = $tmp3[1];
				$lst[$tmp3[0]][] = $tmp2;
					
			}
		}
		return $lst;
	}


	//用户余额请求[4 OK]
	static function getBalance($method='',$info=[]){
		// print_r($method);
		// print_r($info);exit;
		//获取API信息
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('channel_type', $info['channel_type'])
					->where('function', $method)
					->find();

		$url = $apiInfo['url'];
		//组合请求地址， 如果有参数
		if($apiInfo['params']){
			$url = $url."?".$apiInfo['params'];
		}

		$url = str_replace('|', '&', $url);
		$url = str_replace('{country}', $info['country_code'], $url);
		$url = str_replace('{service}', $info['server_code'], $url);

		$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$info['channel_id'])
				->orderRaw("rand()")
				->find();
		if($keys){
			$url = str_replace('{apiKey}', $keys['key'], $url);
			$url = str_replace('{username}', $keys['user_name'], $url);
		}
		
		$header = [];
		if($apiInfo['header']){
			// 通道3 https://5sim.net
			$headerStr = $apiInfo['header'];
			// echo $headerStr;
			$headerStr = str_replace('{apiKey}', $keys['key'], $headerStr);
			// echo $headerStr;
			$header = explode('|', $headerStr);
			sys_log(json_encode($header),'header.log');
		}

		// echo $url; exit;
		sys_log($url,'balance.log');
		if($info['channel_id']==4){
			// $rs = curlGet($url, $header);
			$r_url = "http://47.251.42.169/api.php?url=".urlencode($url);
			// urldecode(str)
			// echo $r_url;
			$rs = file_get_contents($r_url);
			// var_dump($rs);
			// echo $rs;
		}else{
			$rs = curlGet($url, $header);
		}
		sys_log($rs,'balance.log');
		$data = json_decode($rs, true);
		//默认第一个通道，返回类型 Array ( [response] => 1 [balance] => 9593.970 )
		if($info['channel_id']==2){
			//第二个通道返回类型，Array ( [balance] => 285 )，组合成此一个类型
			if($data && isset($data['balance'])){
				$data['response'] = 1;
			}
		}

		if($info['channel_id']==4){
			$tmp = explode('|', $rs);
			if(count($tmp)>0){
				$data['response'] = 1;
				$data['balance'] = $tmp[1];
			}
		}
		return $data;

	}

	//获取服务价格
	static function getServicePrice($method='',$info=[]){
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('channel_type', $info['channel_type'])
					->where('function', $method)
					->find();
		if($info['channel_id']==2){
			$info['country_code'] = strtolower($info['country_code']);
		}
		//组合请求地址
		$url = $apiInfo['url']."?".$apiInfo['params'];
		$url = str_replace('|', '&', $url);
		$url = str_replace('{country}', $info['country_code'], $url);
		$url = str_replace('{service}', $info['server_code'], $url);
		//获取随机key
		$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$info['channel_id'])
				->orderRaw("rand()")
				->find();
		if($keys){
			$url = str_replace('{apiKey}', $keys['key'], $url);
		}
		
		sys_log($url,'url.log');
		if($info['channel_id']==4){
			$r_url = "http://47.251.42.169/api.php?url=".urlencode($url);
			// urldecode(str)
			// echo $r_url;
			$rs = file_get_contents($r_url);
			$data = self::td4clprice($rs, $info);
		}else{
			$rs = curlGet($url);
			$data = json_decode($rs, true);
		}
		
		if($info['channel_id']==2){
			if($data && isset($data['price'])){
				$data['response'] = 1;
				$data['country'] = ''; //此字段不用，为了统一加的
				$data['service'] = ''; //此字段不用，为了统一加的
			}
		}
		return $data;
	}

	static function td4clprice($rs, $info=[]){
		if(!$rs){
			return false;
		}

		$data = [];
		$tmp = explode('|', $rs);
		$data['service'] = $tmp[0];
		$data['server_name'] = $tmp[1];
		for($i=2; $i<$tmp; $i++){
			$tmp2 = explode('-', $tmp[$i]);
			if($tmp2[0] == $info['country_code'] && isset($tmp2[1])){
				$data['response'] = 1;
				$data['country'] = $tmp2[0];
				$data['price'] = $tmp2[1];
				if($tmp2[1]>0){
					$data['count'] = 100;
				}else{
					$data['count'] = 0;
				}
				break;
			}
		}

		return $data;

	}
	//接收特定服务的短信
	static function getSms($method='',$info=[]){
		// http://simsms.org/priemnik.php?metod=get_sms&country=ru&service=opt4&id=25623&apikey=DSWAFvdedrE4
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('channel_type', $info['channel_type'])
					->where('function', $method)
					->find();
		if($info['channel_id']==4){
			$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$info['channel_id'])
				->where('key', $info['api_key'])
				->find();
			if(!$keys){
				return false;
			}
			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{number}', $info['phone'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
			$url = str_replace('{username}', $keys['user_name'], $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$r_url = "http://47.251.42.169/api.php?url=".urlencode($url);
			$rs = file_get_contents($r_url);
			$data = self::td4clsms($rs);

			// echo $url;exit;
		}else{
			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			$url = str_replace('{apiid}', $info['api_id'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);

			$rs = curlGet($url);
			$data = json_decode($rs, true);
		}
		
		sys_log($url,'getSms.log');
		
		sys_log($rs,'getSms.log');
		
		if($info['channel_id']==2){
			$data['response'] = 2;
			if(isset($data['smsCode']) && $data['smsCode']!=null && $data['smsCode']!=""){
				$data['response'] = 1;
				$data['sms'] = $data['smsCode'][0];
				$data['text'] = "sms code : ". $data['smsCode'][0];
			}else{
				$data['sms'] = null;
				$data['text'] = null;
			}
		}elseif($info['channel_id']==3){
			$tmp = [];
			$tmp['sms'] = null;
			$tmp['text'] = null;
			$tmp['response'] = 2;
			if(isset($data[0]) && isset($data[0]['msg']) && $data[0]['msg']!=null){
				$tmp['response'] = 1;
				$tmp['sms'] = $data[0]['msg'];
				$tmp['text'] = "sms code : ". $data[0]['msg'];
			}
		}
		return $data;
	}

	static function td4clsms($rs){
		if(!$rs){
			return false;
		}

		$data = [];
		$arr = explode('|', $rs);
		if($arr[0]==1){
			$data['response'] = 1;
			$data['sms'] = $arr[1];
			$data['text'] = $arr[1];
		}else{
			$data['response'] = 0;
			$data['sms'] = "";
			$data['text'] = $arr[1];
		}

		return $data;
	}

	//取消收到号码的订单
	static function denial($method='',$info=[]){
		// http://simsms.org/priemnik.php?metod=denial&country=ru&service=opt4&id=25623&apikey=DSWAFvdedrE4
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('function', $method)
					->find();
		if(!$apiInfo){
			return false;
		}
		//组合请求地址
		$url = $apiInfo['url']."?".$apiInfo['params'];
		$url = str_replace('|', '&', $url);
		$url = str_replace('{country}', $info['country_code'], $url);
		$url = str_replace('{service}', $info['server_code'], $url);
		$url = str_replace('{apiid}', $info['api_id'], $url);
		$url = str_replace('{apiKey}', $info['api_key'], $url);
		sys_log($url,'denial_user.log');
		$rs = curlGet($url);
		$data = json_decode($rs, true);
		if($info['channel_id']==2){
			if($data['status']=="update"){
				$data['response'] = 1;
			}else{
				$data['response'] = 2;
			}
		}
		sys_log($rs,'denial_user.log');
		return $data;
	}

	//加入黑名单
	static function ban($method='',$info=[]){
		//http://simsms.org/priemnik.php?metod=ban|service={service}|apikey={apiKey}|id={apiid}
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('channel_type', $info['channel_type'])
					->where('function', $method)
					->find();
		if($info['channel_id']==4){
			$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$info['channel_id'])
				->where('key', $info['api_key'])
				->find();
			if(!$keys){
				return false;
			}

			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{username}', $keys['user_name'], $url);
			$url = str_replace('{number}', $info['phone'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			$url = str_replace('{apiid}', $info['api_id'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$r_url = "http://47.251.42.169/api.php?url=".urlencode($url);
			$rs = file_get_contents($r_url);
			sys_log($rs,'ban_user.log');
			$data = self::td4clban($rs);
		}else{
			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			$url = str_replace('{apiid}', $info['api_id'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
			// echo $url;
			sys_log($url,'ban_user.log');
			$rs = curlGet($url);
			$data = json_decode($rs, true);
		}
		
		if($info['channel_id']==2){
			if($data['status']=="update"){
				$data['response'] = 1;
			}else{
				$data['response'] = 2;
			}
		}
		sys_log($url,'ban_user.log');
		sys_log($rs,'ban_user.log');
		return $data;
	}

	static function td4clban($rs){
		if(!$rs){
			return false;
		}

		$data = [];
		$arr = explode('|', $rs);
		if($arr[0]==1){
			$data['response'] = 1;
			$data['text'] = $arr[1];
		}else{
			$data['response'] = 0;
			$data['text'] = isset($arr[1])?$arr[1]:$arr[0];
		}

		return $data;
	}

	//获取服务手机号码
	static function getNumber($method='',$info=[]){
		// http://simsms.org/priemnik.php?metod=get_number&country=RU&service=opt4&apikey=DSWAFvdedrE4
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('channel_type', $info['channel_type'])
					->where('function', $method)
					->find();
		if($info['channel_id']==4){
			//获取key对应的记录
			$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$info['channel_id'])
				->where('key', $info['api_key'])
				->find();
			if(!$keys){
				return false;
			}

			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
			$url = str_replace('{username}', $keys['user_name'], $url);

		}else{
			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
		}
		
		if($info['channel_id']==4){
			$r_url = "http://47.251.42.169/api.php?url=".urlencode($url);
			$rs = file_get_contents($r_url);
			$data = [];
			if($rs>0){
				$data['response'] = 1;
				$data['number'] = $rs;
				$data['id'] = '';
			}else{
				$data['response'] = 0;
				$data['number'] = "";
				$data['id'] = '';
			}
		}else{
			$rs = curlGet($url);
			$data = json_decode($rs, true);
		}
		
		// return ['response'=>1,'number'=>9854521,'id'=>55254];
		if($info['channel_id']==2 && isset($data['tel'])){
			$data['response'] = 1;
			$data['number'] = $data['tel'];
			$data['id'] = $data['idNum'];
		}

		if($info['channel_id']==3){
			
		}
		
		sys_log($url,'number.log');
		sys_log($rs,'number.log');
		return $data;

	}

	//获取可用的服务
	static function getCountNew($method, $info=[]){
		//http://simsms.org/priemnik.php？metod=get_count&country=RU&service=opt28&apikey=key
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('function', $method)
					->find();
		//组合请求地址
		$url = $apiInfo['url']."?".$apiInfo['params'];
		$url = str_replace('|', '&', $url);
		$url = str_replace('{country}', $info['country_code'], $url);
		$url = str_replace('{service}', $info['server_code'], $url);
		$url = str_replace('{apiKey}', $info['api_key'], $url);
		$rs = curlGet($url);
		$data = json_decode($rs, true);
		// return ['response'=>1,'number'=>9854521,'id'=>55254];
		return $data;
	}

	//二次购买【废弃】
	//检查号码以在一个订单内连续接收多条短信
	static function getClearsms($method, $info=[]){
		// http://simsms.org/priemnik.php?metod=get_clearsms&service=opt4&id=25623&apikey=DSWAFvdedrE4.
		//metod=get_clearsms|service={service}|id={apiid}|apikey={apiKey}
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('function', $method)
					->find();
		//组合请求地址
		$url = $apiInfo['url']."?".$apiInfo['params'];
		$url = str_replace('|', '&', $url);
		$url = str_replace('{country}', $info['country_code'], $url);
		$url = str_replace('{service}', $info['server_code'], $url);
		$url = str_replace('{apiid}', $info['api_id'], $url);
		$url = str_replace('{apiKey}', $info['api_key'], $url);
		sys_log($url,'ercigoumai_user.log');
		$rs = curlGet($url);
		$data = json_decode($rs, true);
		sys_log($rs,'ercigoumai_user.log');
		// return ['response'=>1,'number'=>9854521,'id'=>55254];
		return $data;

	}
	//检查是否可以二次购买
	static function getProverka($method, $info=[]){
		// print_r($info);
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('function', $method)
					->find();
		//组合请求地址
		$url = $apiInfo['url']."?".$apiInfo['params'];
		$url = str_replace('|', '&', $url);
		$url = str_replace('{country}', $info['country_code'], $url);
		$url = str_replace('{service}', $info['server_code'], $url);
		$url = str_replace('{apiid}', $info['api_id'], $url);
		$url = str_replace('{apiKey}', $info['api_key'], $url);
		$url = str_replace('{number}', $info['phone'], $url);
		sys_log($url,'Proverka_user.log');
		// echo $url;
		$rs = curlGet($url);
		$data = json_decode($rs, true);
		sys_log($rs,'Proverka_user.log');
		// return ['response'=>1,'number'=>9854521,'id'=>55254];
		return $data;
	}


	//二次购买
	static function getNumberSecond($method, $info=[]){
		// print_r($info);
		$apiInfo = Db::name('channel_function')
					->where('channel_id', $info['channel_id'])
					->where('channel_type', $info['channel_type'])
					->where('function', $method)
					->find();

		if($info['channel_id']==4){
			//获取key对应的记录
			$keys = Db::name('channel_key')
				->where('status',1)
				->where('channel_id',$info['channel_id'])
				->where('key', $info['api_key'])
				->find();
			if(!$keys){
				return false;
			}
			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{username}', $keys['user_name'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
			$url = str_replace('{number}', $info['phone'], $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$r_url = "http://47.251.42.169/api.php?url=".urlencode($url);
			$rs = file_get_contents($r_url);
			$data = [];
			if($rs>0){
				$data['response'] = 1;
				$data['number'] = $rs;
				$data['id'] = '';
			}else{
				$data['response'] = 0;
				$data['number'] = "";
				$data['id'] = '';
			}

		}else{
			//组合请求地址
			$url = $apiInfo['url']."?".$apiInfo['params'];
			$url = str_replace('|', '&', $url);
			$url = str_replace('{country}', $info['country_code'], $url);
			$url = str_replace('{service}', $info['server_code'], $url);
			// $url = str_replace('{apiid}', $info['api_id'], $url);
			$url = str_replace('{apiKey}', $info['api_key'], $url);
			$url = str_replace('{number}', $info['phone'], $url);
			sys_log($url,'ercigoumai_user.log');
			// echo $url;
			$rs = curlGet($url);
			$data = json_decode($rs, true);
		}
		
		sys_log($rs,'ercigoumai_user.log');
		if($info['channel_id']==2){
			$data['response'] = 2;
			$data['number'] = "";
			$data['id'] = "";
			if(isset($data['tel'])){
				$data['number'] = $data['tel'];
				$data['id'] = $data['idNum'];
				$data['response'] = 1;
			}
		}
		sys_log($url,'ercigoumai_user.log');
		// return ['response'=>1,'number'=>9854521,'id'=>55254];
		return $data;
	}

}